{% macro render_checkbox(field) %}
<div class="control-group{% if field.errors %} error{% endif %}">
    <div class="controls">
        <label for="{{ field.id }}" class="checkbox inline">
            {{ field }}{{ field.label.text }}
        </label>
        <span class="help-inline">{{ field.description }}</span>
        {% if field.errors -%}
            {% for error in field.errors -%}
            <span class="help-block error">{{ error|e }}</span>
            {%- endfor %}
        {%- endif %}
    </div>
</div>
{% endmacro%}

{% macro render_radio(field) %}
<div class="control-group{% if field.errors %} error{% endif %}">
    {{ field.label(class_="control-label") }}
    <div class="controls">
        {% for subfield in field -%}
        <label for="{{ subfield.id }}" class="radio inline">
            {{ subfield }} {{ subfield.label.text }}
        </label>
        {%- endfor %}
        <span class="help-inline">{{ field.description }}</span>
        {% if field.errors -%}
            {% for error in field.errors -%}
            <span class="help-block error">{{ error }}</span>
            {%- endfor %}
        {%- endif %}
    </div>
</div>
{% endmacro %}

{% macro render_datepicker(field) %}
<div class="control-group{% if field.errors %} error{% endif %}">
    <label class="control-label" 
        for="{{ field.id }}">
        {{ field.label }}
    </label>
    <div class="controls">
        {{ field() }}
        <span class="help-inline">{{ field.description }}</span>
        {% if field.errors -%}
            {% for error in field.errors -%}
            <span class="help-block error">{{ error }}</span>
            {%- endfor %}
        {%- endif %}
    </div>
</div>
{% endmacro %}

{% macro render_textarea(field) %}
<div class="control-group{% if field.errors %} error{% endif %}">
    <label class="control-label" 
        for="{{ field.id }}">
        {{ field.label }}
    </label>
    <div class="controls">
        {{ field(class="span6", rows="5") }}
        <span class="help-inline">{{ field.description }}</span>
        {% if field.errors -%}
            {% for error in field.errors -%}
            <span class="help-block error">{{ error }}</span>
            {%- endfor %}
        {%- endif %}
    </div>
</div>
{% endmacro %}

{% macro render_input(field) %}
<div class="control-group{% if field.errors %} error{% endif %}">
    <label class="control-label" 
        for="{{ field.id }}">
        {{ field.label }}
    </label>
    <div class="controls">
        {{ field }}
        <span class="help-inline">{{ field.description }}</span>
        {% if field.errors -%}
            {% for error in field.errors -%}
            <span class="help-block error">{{ error }}</span>
            {%- endfor %}
        {%- endif %}
    </div>
</div>
{% endmacro %}

{% macro render_action(field) %}
<div class="control-group">
    <div class="form-actions">
        {{ field(class_="btn btn-primary") }}
    </div>
</div>
{% endmacro %}

{% macro render_form(url, form, horizontal=False, legend=None, confirm_msg=None, formid=None) %}
{% set idattr = "id=" + formid if formid else "" %}
<form {{idattr}} class="{% if horizontal %}form-horizontal{% endif %}" method="POST" action="{{ url }}"
    {% if confirm_msg %}onsubmit='return confirm("{{ confirm_msg }}");' {% endif %} 
    {% if form.multipart %}enctype="multipart/form-data"{% endif %} >
    {{ form.hidden_tag() }}
    <fieldset>
        {% if legend %}
        <legend>{{ legend|safe }}</legend>
        {% endif %}
        {% set focus = True %}
        {% for field in form %}
            {% if field.type != "HiddenField" and field.type != "CSRFTokenField" %}
                {% if field.type == "RadioField" %}
                    {{ render_radio(field) }}
                {% elif field.type == "BooleanField" %}
                    {{ render_checkbox(field) }}
                {% elif field.type == "SubmitField" %}
                    {{ render_action(field) }}
                {% elif field.type == "TextAreaField" %}
                    {{ render_textarea(field) }}
                {% elif field.type == "DateField" %}
                    {{ render_datepicker(field) }}
                {% elif field.type == "TextField" %}
                    {% if focus %}
                        {{ render_input(field) }}
                        {% set focus = False %}
                    {% else %}
                        {{ render_input(field) }}
                    {% endif %}
                {% else %}
                    {{ render_input(field) }}
                {% endif %}
            {% endif %}
        {% endfor %}
    </fieldset>
</form>
{% endmacro %}
